<?php

namespace Member\Controller;

/**
 * 会员控制器
 *
 * @since 1.0 <2016-8-22> sutroon Added.
 */
class Member extends AppbaseController {

    function indexSearch() {
        $search = I('param.');
        if (!empty($search['word'])) {
            $word = $search['word'];
            $where['_string'] = "user_name like '%$word%' or name like '%$word%' or mobile like '%$word%'";
        }
        if (!empty($search['user_name'])) {
            $where['user_name'] = array('like', '%' . $search['user_name'] . '%');
        }
        $search['type_name'] = 'MEMBER';
        $where['type_name'] = $search['type_name'];
        // $where['site_id'] = $this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    public function index() {
        $asearch = $this->indexSearch();
        $m_member = new \XWAM\Model\MemberModel();
        $list = $m_member->selectPaging($page, array('where' => $asearch['where'], 'order' => 'user_name'), array('page_params' => $asearch['search']));
        $this->assign('search', $asearch['search']);
        $this->assign('list', $list);
        $this->assign('page', $page->showBootstrap());
        $this->display();
    }

    function getItemJson($id = '', $username = '') {
        $where = array();
        if ($id) {
            $where = array('id' => $id);
        }
        if ($username) {
            $where = array('user_name' => $username);
        }
        if (!$where) {
            $this->ajaxReturn(null);
        }
        $m_member = new \XWAM\Model\MemberModel();
        $this->ajaxReturn($m_member->where($where)->find());
    }

    public function exportAction() {
        $m_user = new \XWAM\Model\UserModel();
        $list = $m_user->select();
        if (!$list) {
            $this->error('找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    /**
     * 编辑个人资料
     * @since 1.0 <2015-11-11> SoChishun Added.
     */
    function profile_edit() {
        $m_member = new \XWAM\Model\MemberModel();
        $data = $m_member->find_seat($this->loginData['id']);
        $this->assign('data', $data);
        $this->display();
    }

    public function profile_edit_save() {
        $m_member = new \XWAM\Model\MemberModel();
        $result = $m_member->saveUser($this->loginData['id']);
        $this->dialogClose($result['status'], array('success' => '保存成功!<br /><span style="font-size:12px">如有变更[来电弹屏模式]内容，需刷新浏览器才会生效!</span>', 'error' => $result['info'], 'success_action' => '!window.parent.location.reload();', 'ajax' => 5));
    }

    public function edit($id = 0) {
        $data = array();
        if ($id) {
            $m_member = new \XWAM\Model\MemberModel();
            $data = $m_member->find($id);
        }
        if (!$data) {
            $data['site_id'] = $this->siteId;
        }
        $this->assign('data', $data);
        $this->display();
    }

    public function editSave() {
        $m_member = new \XWAM\Model\MemberModel();
        $result = $m_member->saveUser($this->loginData['id']);
        $this->dialogJump($result['status'], $result['info']);
    }

    public function delete($id = '') {
        if (!$id) {
            $this->ajaxMsg(false, '编号丢失!');
        }
        $m_member = new \XWAM\Model\MemberModel();
        $result = $m_member->delete_user($id, $this->loginData['id']);
        $this->ajaxReturn(xsfn_message($result, '删除{%}!'));
    }

    public function reset_password($id) {
        if (!$id) {
            $this->ajaxMsg(false, '参数有误!');
        }
        $m_member = new \XWAM\Model\MemberModel();
        $result = $m_member->where(array('id' => $id))->setField('password', '123456');
        $this->ajaxMsg($result, '密码重置{%}!');
    }

}
